package com.education.auth.service;

import com.education.auth.client.UserClient;
import com.education.auth.config.JwtProperties;
import com.education.auth.entity.UserInfo;
import com.education.auth.utlis.JwtUtils;
import com.education.common.utils.DateUtil;
import com.education.common.utils.StringUtils;
import com.education.user.pojo.User;
import com.education.user.pojo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: ly
 * @create: 2018-07-07 18:01
 **/
@Service
@EnableConfigurationProperties(JwtProperties.class)
public class AuthService {

    @Autowired
    private JwtProperties prop;

    @Autowired
    private UserClient userClient;

    public UserVO expire(String expireTime, UserVO vo) {
        if (StringUtils.isNotEmpty(expireTime)) {
            Date time = new Date();//当前时间
            long current = time.getTime();
            String[] split = expireTime.split(",");
            long strat = DateUtil.parse(split[0]).getTime();//到期时间
            long end = DateUtil.parse(split[1]).getTime();//到期时间
            if (current > end || current < strat) {
                vo.setCode(600);
                return vo;
            }
        }
        return null;
    }

    public UserVO login(String username, String password) {
        try {
            UserVO vo = new UserVO();
            // 校验用户名密码
            User user = this.userClient.queryUserByUsernameAndPassword(username, password);
            String expireDate = user.getExpireDate();
            expire(expireDate, vo);
            //判断是否是学生
            if (user.getRoleId() == null || user.getRoleId() != 3) {
                //账号被停用
                vo.setCode(501);
                return vo;
            }

            if (user == null) {
                return null;
            }
            if (1 == user.getSysState()) {
                //账号被停用
                vo.setCode(500);
                return vo;
            }
            // 生成token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(user.getId());
            userInfo.setUsername(user.getSysRelname());
            userInfo.setUserPhone(user.getSysPhone());
            String token = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
            vo.setToken(token);
            BeanUtils.copyProperties(user, vo);
            // 返回
            return vo;
        } catch (Exception e) {
            return null;
        }
    }

    public UserVO loginTea(String username, String password) {
        try {
            UserVO vo = new UserVO();
            // 校验用户名密码
            User user = this.userClient.queryUserByUsernameAndPassword(username, password);
            expire(user.getExpireDate(), vo);
            //判断是否是学生
            if (user.getRoleId() != null && user.getRoleId() == 4) {
                //账号被停用
                vo.setCode(501);
                return vo;
            }

            if (user == null) {
                return null;
            }
            if (1 == user.getSysState()) {
                //账号被停用
                vo.setCode(500);
                return vo;
            }
            // 生成token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(user.getId());
            userInfo.setUsername(user.getSysRelname());
            userInfo.setUserPhone(user.getSysPhone());
            String token = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
            vo.setToken(token);
            BeanUtils.copyProperties(user, vo);
            // 返回
            return vo;
        } catch (Exception e) {
            return null;
        }
    }

    public UserVO loginJinRan(String username, String password) {
        try {
            UserVO vo = new UserVO();
            // 校验用户名密码
            User user = this.userClient.queryUserByUsernameAndPassword(username, password);
            expire(user.getExpireDate(), vo);
            //判断是否是学生
            if (user.getRoleId() == null) {
                //账号被停用
                vo.setCode(501);
                return vo;
            }

            if (user == null) {
                return null;
            }
            if (1 == user.getSysState()) {
                //账号被停用
                vo.setCode(500);
                return vo;
            }
            // 生成token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(user.getId());
            userInfo.setUsername(user.getSysRelname());
            userInfo.setUserPhone(user.getSysPhone());
            String token = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
            vo.setToken(token);
            BeanUtils.copyProperties(user, vo);
            // 返回
            return vo;
        } catch (Exception e) {
            return null;
        }
    }

    public UserVO login1(String username, String password) {
        try {
            UserVO vo = new UserVO();
            // 校验用户名密码
            User appUser = this.userClient.queryUserByUsernameAndPassword1(username, password);
            expire(appUser.getExpireDate(), vo);

            if (appUser == null) {
                return null;
            }
            //生成token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(appUser.getId());
            String token = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
            vo.setToken(token);
            BeanUtils.copyProperties(appUser, vo);
            // 返回
            return vo;
        } catch (Exception e) {
            return null;
        }
    }

    public UserVO login2(String username, String password) {
        try {
            UserVO vo = new UserVO();
            // 校验用户名密码
            User appUser = this.userClient.queryUserByUsernameAndPassword2(username, password);
            expire(appUser.getExpireDate(), vo);

            if (appUser == null) {
                return null;
            }
            // 生成token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(appUser.getId());
            String token = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
            vo.setToken(token);
            BeanUtils.copyProperties(appUser, vo);
            // 返回
            return vo;
        } catch (Exception e) {
            return null;
        }
    }


    public String getToken(Integer userId) {
        try {
            // 生成token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userId.longValue());
            String token = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
            return token;
        } catch (Exception e) {
            return null;
        }
    }

    public boolean extisUser(String sysPhone) {
       return this.userClient.extisUser(sysPhone);

    }

    public boolean satisfyLogin(User user) {
        return this.userClient.satisfyLogin(user);
    }

    /*public boolean existUser(String sysPhone) {
        this.userClient.
        return false;
    }*/

}
